import SwiftUI

struct UserProfileView: View {
    @State private var selectedTab = 0
    @State private var userProfile = UserProfile.sample
    @Binding var showModelConfig: Bool
    
    init(showModelConfig: Binding<Bool> = .constant(false)) {
        self._showModelConfig = showModelConfig
    }
    
    var body: some View {
        VStack(spacing: 0) {
            // 工具栏
            HStack(spacing: 20) {
                Spacer()
                
                Button(action: {}) {
                    Image(systemName: "trash")
                        .foregroundColor(.gray)
                }
                .buttonStyle(PlainButtonStyle())
                
                Button(action: {}) {
                    Image(systemName: "arrow.clockwise")
                        .foregroundColor(.gray)
                }
                .buttonStyle(PlainButtonStyle())
                
                Button(action: {}) {
                    Image(systemName: "plus")
                        .foregroundColor(.gray)
                }
                .buttonStyle(PlainButtonStyle())
                
                Button(action: {
                    withAnimation {
                        showModelConfig.toggle()
                    }
                }) {
                    Image(systemName: "gearshape")
                        .foregroundColor(.gray)
                }
                .buttonStyle(PlainButtonStyle())
            }
            .padding(.horizontal)
            .padding(.top, -3)
            
            // 顶部标题
            Text("上下文窗口")
                .font(.headline)
                .padding(.horizontal)
                .padding(.top, 0)
                .padding(.bottom, 2)
                .frame(maxWidth: .infinity, alignment: .leading)
            
            // 字符计数
            HStack {
                Text(userProfile.characterCountText)
                    .font(.caption)
                    .foregroundColor(.gray)
                
                Spacer()
                
                Text("令牌")
                    .font(.caption)
                    .foregroundColor(.gray)
            }
            .padding(.horizontal)
            .padding(.top, -2)
            .padding(.bottom, 2)
            
            // 进度条
            ProgressBar(value: Double(userProfile.usedCharacters) / Double(userProfile.totalCharacters))
                .frame(height: 5)
                .padding(.horizontal)
                .padding(.bottom, 5)
            
            // 标签选择器
            HStack {
                Picker("记忆类型", selection: $selectedTab) {
                    Text("核心记忆 (2)").tag(0)
                    Text("归档记忆 (0)").tag(1)
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding()
            }
            
            // 记忆内容
            ZStack {
                if selectedTab == 0 {
                    TextEditor(text: $userProfile.coreMemory)
                        .padding(5)
                        .background(Color(red: 0.95, green: 0.95, blue: 0.95))
                        .cornerRadius(8)
                        .padding(.horizontal)
                } else {
                    Text("没有归档记忆")
                        .foregroundColor(.gray)
                        .padding()
                }
            }
            .frame(height: 250)
            
            Spacer()
            
            // 标签选择器
            HStack {
                Picker("视图类型", selection: .constant(0)) {
                    Text("用户").tag(0)
                    Text("高级").tag(1)
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding()
            }
            
            // 个人资料
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("Human")
                        .font(.headline)
                    
                    Spacer()
                    
                    Button(action: {}) {
                        Image(systemName: "pencil")
                            .foregroundColor(.blue)
                            .font(.caption)
                    }
                    .buttonStyle(PlainButtonStyle())
                }
                
                Text("\(userProfile.usedCharacters)/\(userProfile.totalCharacters) 字符")
                    .font(.caption)
                    .foregroundColor(.gray)
                
                Text(userProfile.coreMemory)
                    .font(.caption)
                    .foregroundColor(.gray)
                    .lineLimit(5)
                
                Text(userProfile.lastUpdatedText)
                    .font(.caption2)
                    .foregroundColor(.gray)
            }
            .padding()
            .frame(maxWidth: .infinity, alignment: .leading)
            .background(Color(red: 0.95, green: 0.95, blue: 0.95))
            .cornerRadius(8)
            .padding(.horizontal)
            
            Spacer()
        }
        .frame(width: 280)
        .background(Color(red: 0.97, green: 0.97, blue: 0.97))
    }
}

struct ProgressBar: View {
    var value: Double
    
    var body: some View {
        GeometryReader { geometry in
            ZStack(alignment: .leading) {
                Rectangle()
                    .frame(width: geometry.size.width, height: geometry.size.height)
                    .opacity(0.1)
                    .foregroundColor(.gray)
                
                Rectangle()
                    .fill(LinearGradient.rainbow)
                    .frame(width: min(CGFloat(self.value) * geometry.size.width, geometry.size.width), height: geometry.size.height)
            }
            .cornerRadius(10)
        }
    }
}

extension LinearGradient {
    static var rainbow: LinearGradient {
        return LinearGradient(
            gradient: Gradient(colors: [.blue, .green, .yellow, .orange, .red]),
            startPoint: .leading,
            endPoint: .trailing
        )
    }
} 
